package com.android.infantschool.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.app.InfantApp;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.ui.activity.AskForLeaveActivity;
import com.android.infantschool.ui.activity.HiddenManagerActivity;
import com.android.infantschool.ui.activity.ParentsBindingActivity;
import com.android.infantschool.ui.activity.PersonInfoActivity;
import com.android.infantschool.ui.activity.SetUpActivity;
import com.android.infantschool.ui.adapter.MeAdapter;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\3 0003 13:08
 * @class describe 我的模块
 */
public class MeFragment extends BaseFragment implements View.OnClickListener, MeAdapter.OnLister {

    private RelativeLayout rlTitle;
    private TextView tvTitle;
    private ImageView imgBack;
    private ImageView imgMeHeader;
    private TextView tvName;
    private TextView tvPhone;
    private RecyclerView recyclerView;
    private MeAdapter adapter;
    private RelativeLayout rlPersonInfo;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rlTitle = view.findViewById(R.id.title);
        tvTitle = view.findViewById(R.id.tv_title);
        imgBack = view.findViewById(R.id.img_back);

        tvTitle.setText("我的");
        imgBack.setVisibility(View.GONE);
        imgMeHeader = (ImageView) view.findViewById(R.id.img_me_header);
        imgMeHeader.setOnClickListener(this);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvName.setOnClickListener(this);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        tvPhone.setOnClickListener(this);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setOnClickListener(this);
        rlPersonInfo = (RelativeLayout) view.findViewById(R.id.rl_person_info);
        rlPersonInfo.setOnClickListener(this);
        tvName.setText(AppConfig.getNickName());
        tvPhone.setText(AppConfig.getUserPhone());

        LinearLayoutManager layoutManager = new LinearLayoutManager(activity) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }

            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
        };

        adapter = new MeAdapter(activity, this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setData(AppDataSource.getMeList());
    }

    @Override
    public void onResume() {
        super.onResume();
        Glide.with(this).load(AppConfig.getUserPhoto())
                .transform(new CenterCrop(activity), new GlideRoundTransform(activity))
                .placeholder(R.mipmap.icon_user)
                .error(R.mipmap.icon_user)
                .into(imgMeHeader);
    }

    @Override
    public void lister(int position) {
        switch (position) {
            case 0://家长绑定
                activity.startActivity(ParentsBindingActivity.newIntent(activity));
                break;
            case 1://请假调休
                activity.startActivity(AskForLeaveActivity.newIntent(activity));
                break;
            case 2://隐患管理
                activity.startActivity(HiddenManagerActivity.newIntent(activity));
                break;
            case 3://设置
                activity.startActivity(SetUpActivity.newIntent(activity));
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_person_info:
                startActivity(PersonInfoActivity.newIntent(activity));
                break;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
